{% extends 'layout/manage.html' %}
{% load static %}
{% load issue %}
{% block css %}
    <link rel="stylesheet" href="{% static 'plugin/editor-md/css/editormd.min.css' %}">
    <!--  时间选择器插件 -->
    <link href="{% static 'plugin/datepicker/css/bootstrap-datepicker.min.css' %}" rel="stylesheet">
    <!--  选择器插件 -->
    <link rel="stylesheet" href="{% static 'plugin/bootstrap-select/dist/css/bootstrap-select.min.css' %}">
    <link rel="stylesheet" href="{% static 'plugin/select2/css/select2.min.css' %}">

    <style>
        .issues-list .number {
            width: 100px;
            text-align: right;
        }

        .issues-list .number a {
            font-weight: 500;
            padding: 0 10px;
        }

        .issues-list .issue .tags {
            padding: 10px 0;
        }

        .issues-list .issue .tags span {
            margin-right: 20px;
            display: inline-block;
            font-size: 12px;
        }

        .issues-list .issue .tags .type {
            color: white;
            padding: 1px 5px;
            border-radius: 5px;
            background-color: #dddddd;
        }

        .editormd {
            margin-bottom: 0;
        }

        .pd-0 {
            padding: 0 !important;
        }

        /* 筛选 */
        .filter-area .item {
            margin-bottom: 15px;
        }

        .filter-area .item .title {
            padding: 5px 0;
        }

        .filter-area .item .check-list a {
            text-decoration: none;
            display: inline-block;
            min-width: 65px;
        }

        .filter-area .item .check-list label {
            font-weight: 200;
            font-size: 13px;
            margin-left: 3px;
        }

        .filter-area .item .check-list a:hover {
            font-weight: 300;
        }

        .filter-area .item .check-list .cell {
            margin-right: 10px;
        }
    </style>
{% endblock %}

{% block content %}
    <div class="container-fluid clearfix" style="padding: 20px 0"></div>
    <div class="col-sm-3">
        <div class="panel panel-default">
                <div class="panel-heading">
                    <i class="fa fa-search" aria-hidden="true"></i> 筛选
                </div>
               <div class="panel-body filter-area">
                   {% for row in filter_list %}
                        <div class="item">
                            <div class="title">{{ row.title }}</div>
                            <div class="check-list">
                                {% for item in row.filter %}
                                    {{ item }}
                                {% endfor %}
                            </div>
                        </div>
                    {% endfor %}
               </div>
            </div>
    </div>
    <div class="col-sm-9">
        <div class="panel panel-default">
                <div class="panel-heading"><i class="fa fa-question" aria-hidden="true"></i> 问题</div>
                <div class="panel-body">
                    <a class="btn btn-success btn-sm" data-toggle="modal" data-target="#addModal">新建问题</a>
                    <a class="btn btn-primary btn-sm">邀请成员</a>
                </div>
                <table class="table">
                    <tbody class="issues-list">
                    {% for issues in issues_list %}
                        <tr>
                            <td class="number">
                                <i class="fa fa-circle text-{{ issues.priority }}"></i>
                                <a href="{% url 'issues_detail' project_id=request.tracer.project.id issues_id=issues.id %}" target="_blank">{% string_just issues.id %}</a>
                            </td>
                            <td class="issue">
                                <div>
                                    <a href="{% url 'issues_detail' project_id=request.tracer.project.id issues_id=issues.id %}" target="_blank">{{ issues.subject }}</a>
                                </div>
                                <div class="tags">
                                    <span class="type"> {{ issues.issue_type.title }}</span>
                                    <span><i class="fa fa-refresh" aria-hidden="true"></i> {{ issues.get_status_display }}</span>
                                    <span><i class="fa fa-user-o" aria-hidden="true"></i> {{ issues.creator }}</span>
                                    {% if issues.assign %}
                                        <span><i class="fa fa-hand-o-right" aria-hidden="true"></i> {{ issues.assign }}</span>
                                    {% endif %}
                                    {% if issues.end_date %}
                                        <span><i class="fa fa-calendar" aria-hidden="true"></i> {{ issues.end_date }} 截止</span>
                                    {% endif %}

                                    <span><i class="fa fa-clock-o" aria-hidden="true"></i> {{ issues.latest_update_datetime }} 更新</span>

                                </div>
                            </td>
                        </tr>
                    {% endfor %}
                    </tbody>

                  </table>

        </div>
            <nav aria-label="...">
              <ul class="pagination" style="margin-top: 0;">
                {{ page_html|safe }}
             </ul>
            </nav>

    </div>

    <!-- Large modal -->
<div class="modal fade bs-example-modal-lg" id="addModal" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel">
  <div class="modal-dialog modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title">新建问题</h4>
      </div>
      <div class="modal-body">

          <form id="addForm" class="form-horizontal">
            {% csrf_token %}
              <div class="form-group">
                <label for="{{ form.issue_type.id_for_label }}" class="col-sm-2 control-label">{{ form.issue_type.label }}</label>
                <div class="col-sm-10">
                    {{ form.issue_type }}
                </div>
                  <span class="error-msg"></span>
              </div>

              <div class="form-group">
                <label for="{{ form.subject.id_for_label }}" class="col-sm-2 control-label">{{ form.subject.label }}</label>
                <div class="col-sm-10">
                    <div>
                    {{ form.subject }}
                    </div>
                    <div class="error-msg"></div>
                </div>

              </div>

            <div class="form-group">
                <label for="{{ form.module.id_for_label }}" class="col-sm-2 control-label">{{ form.module.label }}</label>
                <div class="col-sm-10">
                    <div>
                        {{ form.module }}
                    </div>
                    <span class="error-msg"></span>
                </div>
              </div>

              <div class="form-group">
                <label for="{{ form.desc.id_for_label }}" class="col-sm-2 control-label">{{ form.desc.label }}</label>
                <div class="col-sm-10">
                    <div id="editor">
                        {{ form.desc }}
                    </div>
                    <span class="error-msg"></span>
                </div>
              </div>

              <div class="form-group clearfix">
                <div class="col-md-6 pd-0">
                    <label for="{{ form.status.id_for_label }}"
                           class="col-md-4 control-label">{{ form.status.label }}</label>
                    <div class="col-md-8 clearfix">
                        <div>
                            {{ form.status }}
                        </div>
                        <div class="error-msg"></div>

                    </div>
                </div>
                <div class="col-md-6 pd-0">
                    <label for=" {{ form.priority.id_for_label }}"
                           class="col-md-4 control-label">{{ form.priority.label }}</label>
                    <div class="col-md-8">
                        <div>
                            {{ form.priority }}
                        </div>
                        <div class="error-msg"></div>


                    </div>
                </div>
            </div>

              <div class="form-group clearfix">
                <div class="col-md-6 pd-0">
                    <label for="{{ form.assign.id_for_label }}"
                           class="col-md-4 control-label">{{ form.assign.label }}</label>
                    <div class="col-md-8 clearfix">
                            {{ form.assign }}
                        <div class="error-msg"></div>
                    </div>
                </div>
                <div class="col-md-6 pd-0">
                    <label for=" {{ form.attention.id_for_label }}"
                           class="col-md-4 control-label">{{ form.attention.label }}</label>
                    <div class="col-md-8">
                            {{ form.attention }}
                        <div class="error-msg"></div>

                    </div>
                </div>
            </div>

              <div class="form-group clearfix">
                <div class="col-md-6 pd-0">
                    <label for="{{ form.start_date.id_for_label }}"
                           class="col-md-4 control-label">{{ form.start_date.label }}</label>
                    <div class="col-md-8 clearfix">
                        <div>
                            <div class="input-group">
                                <span class="input-group-addon" id="sizing-addon2">
                                    <i class="fa fa-calendar" aria-hidden="true"></i>
                                </span>
                                {{ form.start_date }}
                            </div>
                            <div class="error-msg"></div>
                        </div>


                    </div>
                </div>
                <div class="col-md-6 pd-0">
                    <label for=" {{ form.end_date.id_for_label }}"
                           class="col-md-4 control-label">{{ form.end_date.label }}</label>
                    <div class="col-md-8">
                        <div>
                            <div class="input-group">
                                <span class="input-group-addon" id="sizing-addon2">
                                    <i class="fa fa-calendar" aria-hidden="true"></i>
                                </span>
                                {{ form.end_date }}
                            </div>
                            <div class="error-msg"></div>
                        </div>


                    </div>
                </div>
            </div>

              <div class="form-group clearfix">
                <div class="col-md-6 pd-0">
                    <label for="{{ form.mode.id_for_label }}"
                           class="col-md-4 control-label">{{ form.mode.label }}</label>
                    <div class="col-md-8 clearfix">
                        <div>
                            {{ form.mode }}
                        </div>
                        <div class="error-msg"></div>


                    </div>
                </div>
                <div class="col-md-6 pd-0">
                    <label for=" {{ form.parent.id_for_label }}"
                           class="col-md-4 control-label">{{ form.parent.label }}</label>
                    <div class="col-md-8">
                        <div>
                            {{ form.parent }}
                        </div>
                        <div class="error-msg"></div>

                    </div>
                </div>
            </div>

            </form>

      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">取 消</button>
        <button type="button" class="btn btn-primary" id="btnAddSubmit">确 定</button>
      </div>
    </div>
  </div>
</div>
{% endblock %}

{% block js %}
    <script src="{% static 'plugin/editor-md/editormd.min.js' %}"></script>
    <script src="{% static 'plugin/datepicker/js/bootstrap-datepicker.min.js' %}"></script>
    <script src="{% static 'plugin/datepicker/locales/bootstrap-datepicker.zh-CN.min.js' %}"></script>
    <script src="{% static 'plugin/bootstrap-select/dist/js/bootstrap-select.min.js' %}"></script>
    <script src="{% static 'plugin/bootstrap-select/dist/js/i18n/defaults-zh_CN.min.js' %}"></script>
    <script src="{% static 'plugin/select2/js/select2.min.js' %}"></script>

    <script>
        var WIKI_UPLOAD_URL = "{% url 'wiki_upload' project_id=request.tracer.project.id %}";
        var POST_ISSUES = "{% url 'issues' project_id=request.tracer.project.id %}";

        $(function () {
            bindBootStrapShownEvent();
            initDatePicker();
            bindBtnAddSubmit();

            bindClickCheckFilter();

            initSelect2();
        });

                function initSelect2() {
            $('.select2').select2({}).on('select2:select', function (e) {
                // 选中某一项触发
                location.href = e.params.data.id;
            }).on('select2:unselect', function (e) {
                // 移除某一项触发
                location.href = e.params.data.id;
            });
        }

        /*
        点击筛选的checkbox
        */
        function bindClickCheckFilter() {
            $('.filter-area').find(':checkbox').click(function () {
                location.href = $(this).parent().attr('href');
            });
        }

        function bindBtnAddSubmit() {
            $('#btnAddSubmit').click(function () {
                $("#addForm").find('.error-msg').empty();

                $.ajax({
                    url: POST_ISSUES,
                    type: "POST",
                    data: $("#addForm").serialize(),
                    dataType: "JSON",
                    success: function (res) {
                        if (res.status){
                            location.reload();
                        }
                        else{
                            $.each(res.error, function (k, v) {
                                $('#id_'+ k).parent().next('.error-msg').text(v[0]);
                            })
                        }
                    }
                })
            })
        }

        function bindBootStrapShownEvent() {
            $('#addModal').on('shown.bs.modal', function (event) {
                // 显示模态对话框的回调函数，弹出对话框时执行
                initEditMd();
            })
        }

        // 初始化datepicker
        function initDatePicker() {

            $('#id_start_date,#id_end_date').datepicker({
                format: 'yyyy-mm-dd',
                startDate: '0',
                language: "zh-CN",
                autoclose: true
            });
        }


        // 初始化md编辑器，将textarea转换为md
        function initEditMd() {
            editormd('editor', {
                placeholder: "请输入内容",
                height:300,
                path: "{% static 'plugin/editor-md/lib/' %}", //组件路径
                imageUpload: true, // 本地上传图片
                imageFormats: ["jpg", "jpeg", "png", "gif", "bmp"],
                imageUploadURL: WIKI_UPLOAD_URL,
                emoji: true,
                taskList: true,
                tocm: true,
                tex: true, // 默认不解析
                flowChart: true, // 默认不解析
                sequenceDiagram: true, // 默认不解析
                codeFold: true,
            })
        }

    </script>
{% endblock %}